var redis = require("redis")
var UUID = require("uuid");
var handler = module.exports
var RDS_PORT = 6379
var RDS_HOST = "120.77.157.99"
// RDS_HOST = "r-wz9a9d0c1d885e84.redis.rds.aliyuncs.com"
var RDS_PWD = "MyRedis2017"
var RDS_OPTS = {auth_pass : RDS_PWD}
var local = {}
var db = redis.createClient(RDS_PORT,RDS_HOST,RDS_OPTS)
db.on("ready",function(res) {
	db.select("15",function(err) {
		local.addGold(10001,10)
		local.addDimond(10001,10)
		local.addLottryCount(10001,10)
		local.sendMail(10001,"邮件标题1111!!!","邮件内容2222!!!",{101:10,102:10})
	})
})

//增加金币
local.addGold = function(uid,value) {
	var info = {
		"type" : "addGold",
		"uid" : uid,
		"value" : value,
		"respond" : false
	}
	local.saveMessage(info)
}
//增加钻石
local.addDimond = function(uid,value) {
	var info = {
		"type" : "addDimond",
		"uid" : 0,
		"value" : value,
		"respond" : true
	}
	local.saveMessage(info)
}
//增加抽奖次数
local.addLottryCount = function(uid,value) {
	var info = {
		"type" : "addLottryCount",
		"uid" : uid,
		"value" : value,
		"respond" : true
	}
	local.saveMessage(info)
}
//发送邮件
local.sendMail = function(uid,title,content,award) {
	var info = {
		"type" : "sendMail",
		"uid" : uid,
		"title" : title,
		"content" : content,
		"award" : award,
		"respond" : true
	}
	local.saveMessage(info)
}
//充值
local.recharge = function(uid,value) {
	// body...
}
//保存指令
local.saveMessage = function(info){
	info.time = (new Date()).getTime()
	info.id = UUID.v1();
	db.rpush("backstageMessage",JSON.stringify(info),function(err,data) {
		if(err){
			console.log(err)
		}
	})
}

